<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="cancleBtn">
    <!-- 表单 -->
    <el-form :model="formData" label-width="120px">
      <el-form-item label="姓名">
        <el-input v-model="formData.username" style="width: 50%" placeholder="请输入姓名" />
      </el-form-item>
     
      <el-form-item label="手机">
        <el-input v-model="formData.mobile" style="width: 50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker v-model="formData.timeOfEntry" style="width: 50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式">
        <el-select v-model="formData.formOfEmployment" style="width: 50%" placeholder="请选择">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号">
        <el-input v-model="formData.workNumber" style="width: 50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门">
        <el-input v-model="formData.departmentName" @focus="getDepartments" style="width: 50%" placeholder="请选择部门" />
        <el-tree v-if="showTree" @node-click="selectNode" :data="treeData" :props="{ label: 'name' }"></el-tree>
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTime" style="width: 50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="cancleBtn">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      EmployeeEnum: EmployeeEnum,
      treeData: [],
      showTree: false
    }
  },
  methods: {
    cancleBtn() {
      // 同步更新数据
      this.$emit('update:showDialog', false)
    },
    async getDepartments() {
      const res = await getDepartments()
      this.treeData = tranListToTreeData(res.data.data.depts, '')
      this.showTree = true
    },
    selectNode(node) {
      console.log('node:', node)
      this.formData.departmentName = node.name
      this.showTree = false
    },
    async btnOK() {
      // 调用新增员工的接口
      await addEmployee(this.formData)
      // 执行父组件的方法 获取最新数据列表
      this.$parent.getEmployeeList()
      this.$parent.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
